﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NextCMS.Models;
using System.IO;

public partial class WebAdmin_ManageWebResources : WebadminBasePage
{
    int Id = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        int.TryParse(Convert.ToString(Request.Form["hidWebResourceId"]), out Id);
        if (!IsPostBack)
        {
            string command = Convert.ToString(Request.Form["hidWebResourceCommand"]);
            if (command == "Save")
            {
                using (SiteResourcesHelper helper = new SiteResourcesHelper())
                {
                    SiteResourceModel srm = new SiteResourceModel();
                    if (Id > 0)
                    {
                        srm = helper.GetById(Id);
                        srm.Name = Convert.ToString(Request.Form["txtWebResourceName"]); 
                        helper.Edit(srm);
                    }
                    else
                    {
                        srm.Name = Convert.ToString(Request.Form["txtWebResourceName"]);
                        string folderName = System.Text.RegularExpressions.Regex.Replace(srm.Name, @"[^\w\.@-]", "");
                        if (CreateFolder("/Templates/", folderName))
                        {
                            srm.FolderName = folderName;
                            srm.SiteId = SiteId;
                            int newid = helper.Save(srm);
                        }
                        else
                        {
                            Page.ClientScript.RegisterStartupScript(Page.GetType(), "f", @"$('#message1').message({ dismiss: false, type: 'error', message: 'Folder Name already exists in system' });", true);
                        }
                    }
                }
            }
            else if (command == "Delete")
            {
                using (SiteResourcesHelper helper = new SiteResourcesHelper())
                {
                    helper.Delete(Id);
                }
            }
            using (SiteResourcesHelper helper = new SiteResourcesHelper())
            {
                GVWebResources.DataSource= helper.GetAllBySiteId(SiteId);
                GVWebResources.DataBind();
            }
        }
        Page.ClientScript.RegisterClientScriptInclude(Page.GetType(), "jquery-1.7.js", ResolveClientUrl("~/Resources/Javascript/jquery-1.7.js"));
        Page.ClientScript.RegisterClientScriptInclude(Page.GetType(), "jquery-ui-1.8.4.custom.min.js", ResolveClientUrl("~/FileManager/js/jquery-ui-1.8.4.custom.min.js"));
        Page.ClientScript.RegisterClientScriptInclude(Page.GetType(), "jquery.ui.message.js", ResolveClientUrl("~/Resources/Javascript/jquery.ui.message.js"));
        AddCssLink("FileManager/css/ui-lightness/jquery-ui-1.8.4.custom.css");
    }
    private bool CreateFolder(string Path, string FolderName)
    {
        string FolderPath = Server.MapPath("~") + Path + FolderName;
        if (Directory.Exists(FolderPath))
            return false;
        else
        {
            Directory.CreateDirectory(FolderPath);
            return true;
        }
    }
}